package org.vectrics.company.web.struts;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Category;
import org.apache.struts.action.ActionMapping;
import org.vectrics.company.Company;
import org.vectrics.domain.Domain;
import org.vectrics.location.Address;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.company.RecruitCompanyInfo;
import org.vectrics.recruiter.company.RecruitCompanyService;
import org.vectrics.struts.Request;
import org.vectrics.struts.UserBufferManager;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;

public class ShowCompanyEditAction  extends VectricsStrutsAction {
    
	private static Category log = Category.getInstance(ShowCompanyEditAction.class);
	public static String JOB_PARAM = "job";

	// Intercept 
	@Override
	protected String doExecute(VectricsActionData actionData) throws JspException {
		CompanyForm companyForm = null;
		
		if (actionData.getForm() == null) {
			companyForm = new CompanyForm();
			actionData.setForm(companyForm);			
		}
		else {
			companyForm = (CompanyForm)actionData.getForm();
		}

		CompanyForm.prepareAttributes(actionData.getRequest());
		Request request = actionData.getRequest();
		ActionMapping mapping = actionData.getMapping();
		
		if ("request".equals(mapping.getScope())) {
			log.debug("Company form attached to request, attribute name = " + mapping.getAttribute());
			request.setAttribute(mapping.getAttribute(), companyForm);
		}
		else {
			request.setSessionAttribute(mapping.getAttribute(), companyForm);
		}
		
		String companyIdParam = request.getParameter("companyId");
		String parentIdParam = request.getParameter("parentId");
		Long companyId = null;
		Long parentId = null;
		if (companyIdParam != null)
		    companyId = new Long(companyIdParam);
		if (parentIdParam != null)
		    parentId = new Long(parentIdParam);
		
		Company company = null;
		try {
			company = CoreServiceLocator.getCompanyService().findCompany(companyId);
		}
		catch (Exception ex) {
			log.error("Exception finding company", ex);
			throw new JspException("Exception finding company", ex);
		}
		
		if (company == null) {
			if (log.isDebugEnabled())
				log.debug("Could not find company id = " + companyId);
			companyForm.setCompanyId(null);
			
			if (parentId != null)
			    companyForm.setParentId(parentId.toString());
			else
			    companyForm.setParentId(null);
			
			if (parentId == null) {
				Domain systemDomain = CoreServiceLocator.getConfiguration().getSystemDomain();
				if (CoreServiceLocator.getConfiguration().getSingleDomain(systemDomain) == false) {
					request.setAttribute("newDomainOption", "true");
				} else {
					request.setAttribute("newDomainOption", "false");
				}
			} else {
				Company parentCompany = CoreServiceLocator.getCompanyService().findCompany(parentId);
				if (CoreServiceLocator.getConfiguration().getSingleDomain(parentCompany.getDomain()) == false) {
					request.setAttribute("newDomainOption", "true");
				} else {
					request.setAttribute("newDomainOption", "false");
					
				}
			}
			
		} else {
			request.setAttribute("newDomainOption", "false");
			log.debug("execute() - company found - populating form fields");
			companyForm.setCompanyId(company.getId().toString());
			companyForm.setName(company.getName());
			companyForm.setSectorCode(company.getSector().getCode());
			companyForm.setReferenceId(company.getReferenceId());
			companyForm.setPhone(company.getPhone());
			companyForm.setPhone2(company.getPhone2());
			companyForm.setLocationId(company.getAddress().getLocation().getId().toString());
			request.setAttribute("locationId", companyForm.getLocationId());
			if (company.getParent() != null)
			    companyForm.setParentId(company.getParent().getId().toString());
			Address address = null;
			address = company.getAddress();
			if (address != null) {
				companyForm.setAddress1(address.getAddress1());
				companyForm.setAddress2(address.getAddress2());
				companyForm.setLocationId(address.getCountryCode());
				companyForm.setPostalCode(address.getPostalCode());
			}
			UserBufferManager bufMgr = new UserBufferManager();
			bufMgr.addBufferedObject(request, company);
			
			RecruitCompanyInfo recruitCompanyInfo = RecruitCompanyService.getInstance().findRecruitCompanyInfo(company);
			if (recruitCompanyInfo != null) {
				log.debug("Company background: " + recruitCompanyInfo.getJobPostCompanyInfo());
				companyForm.setBackground(recruitCompanyInfo.getJobPostCompanyInfo());
			}
		}

		
		
		if (log.isDebugEnabled()) {
			log.debug("execute() - form.id" + companyForm.getCompanyId());
			log.debug("execute() - form.name" + companyForm.getName());
		}
		
		saveToken(request.getHttpRequest());
				 	
		return ("success");
	}


}
